/**
  * Created by Joshua Conero(O'Uymas) on 2016/12/7 0007.
  * QQ: 957376074
  * E-mail: Brximl@163.com
  */
// 时间运行比较
package conero
import java.text.{DecimalFormat, SimpleDateFormat}
import java.util.Date
import scala.util.Try
class dateDiff{
  // 系统起始时间
  val dt = new Date()
  val sysTimeBegin = dt.getTime
  // 单位-毫秒差
  def diffTime():Long ={
    val now = new Date()
    now.getTime - sysTimeBegin
  }
  def getStartTime(fmt:String = ""):String = {
    val format = if(fmt == "") "yyyy-MM-dd HH:mm:ss" else fmt
    val ft = new SimpleDateFormat(format)
    ft.format(dt)
  }
  // 时分秒转变为 秒速字符串
  def time2Str():String ={
    val mesc = diffTime()
    val ms = mesc/1000
    val day = Math.floor(ms/(24*60*60))
    var remainder = ms%(24*60*60)
    val hour = Math.floor(remainder/(60*60))
    remainder = remainder%(60*60)
    val min = Math.floor(remainder/60)
    remainder = remainder%(60)
    val sec = remainder
    day.toInt.toString+"天 "+hour.toInt.toString+"小时 "+min.toInt.toString+"分钟 "+sec.toString+"秒"
  }
  // 格式化求时间差
  def diffTime(stype:String = "mesc"):Float ={
    var vtype = stype.toLowerCase
    val mesc = diffTime()
    val bit = new DecimalFormat(".0000")
    vtype = vtype match {
      case "d"=> "day"
      case "h"=> "hour"
      case "m"=> "minute"
      case "min"=> "minute"
      case "s"=> "second"
      case "sec"=> "second"
      case _ => vtype
    }
    vtype match {
      case "day" =>
        val num = (mesc/(24*60*60*1000)).toFloat
        Try(bit.format(num).trim.toFloat).toOption.last
      case "hour" =>
        val num = (mesc/(60*60*1000)).toFloat
        Try(bit.format(num).trim.toFloat).toOption.last
      case "minute" =>
        val num = (mesc/(60*1000)).toFloat
        Try(bit.format(num).trim.toFloat).toOption.last
      case "second" =>
        val num = (mesc/(1000)).toFloat
        Try(bit.format(num).trim.toFloat).toOption.last
      case _ => Try(bit.format(mesc).trim.toFloat).toOption.last
    }
  }
  // date 对象
  def date():Date = new Date()
}
